package com.foo.DataStoreTest;

import java.sql.Date;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.*;

import junit.framework.Assert;
import org.junit.Test;
import com.foo.Entity.*;

public class StockSenderClientTest 
{
	Queue queue = null;
	QueueConnectionFactory factory = null;
	QueueConnection connection = null;
	QueueSender sender = null;
	QueueSession session = null;
	Queue replyQueue = null;
	private MessageConsumer consumer;
	
	@Test
	public void DataStoreTestPositive() {
		try {
			Properties props = new Properties();
			props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			props.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(props);
			factory = (QueueConnectionFactory) ctx.lookup("XAConnectionFactory");
			connection = factory.createQueueConnection();
			session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
			queue = (Queue) ctx.lookup("queue/HistoricalDataStorage");
			sender = session.createSender(queue);
			
			connection.start();
			
			//get the reply
			replyQueue = session.createTemporaryQueue();		
			consumer = session.createConsumer( replyQueue );

			HistoricalData[] historical;
			historical = new HistoricalData[1];
			historical[0] = new HistoricalData();
			historical[0].setDeals_date(Date.valueOf("2012-09-19"));
			historical[0].setTicker_symbol("ZZZZZ");
			historical[0].setOpen_value(50);
			historical[0].setClose_value(45);
			historical[0].setHigh_value(51);
			historical[0].setLow_value(49);
			historical[0].setVolume(555);
			
			//create and set a message to send
			ObjectMessage message = session.createObjectMessage();
			message.setObject(historical);
			message.setJMSReplyTo(replyQueue);
			
			sender.send(message);
			System.out.println("Sending message");   
			
			TextMessage Reply = (TextMessage)consumer.receive();
			int sum = Integer.parseInt( Reply.getText() );
			
			Assert.assertTrue(sum == 1);
			
			session.close ();
			connection.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void DataStoreTestNegative() {
		try {
			Properties props = new Properties();
			props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			props.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(props);
			factory = (QueueConnectionFactory) ctx.lookup("XAConnectionFactory");
			connection = factory.createQueueConnection();
			session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
			queue = (Queue) ctx.lookup("queue/HistoricalDataStorage");
			sender = session.createSender(queue);
			
			connection.start();
			
			//get the reply
			replyQueue = session.createTemporaryQueue();		
			consumer = session.createConsumer( replyQueue );

			HistoricalData[] historical;
			historical = new HistoricalData[0];
			/*
			historical[0] = new HistoricalData();
			historical[0].setDeals_date(Date.valueOf("2012-09-19"));
			historical[0].setTicker_symbol("ZZZZZ");
			historical[0].setOpen_value(50);
			historical[0].setClose_value(45);
			historical[0].setHigh_value(51);
			historical[0].setLow_value(49);
			historical[0].setVolume(555);
			*/
			
			//create and set a message to send
			ObjectMessage message = session.createObjectMessage();
			message.setObject(historical);
			message.setJMSReplyTo(replyQueue);
			
			sender.send(message);
			System.out.println("Sending message");   
			
			TextMessage Reply = (TextMessage)consumer.receive();
			int sum = Integer.parseInt( Reply.getText() );
			
			Assert.assertTrue(sum == 0);
			
			session.close ();
			connection.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
